package com.xinghanlaser.applet.base.auth.controller;

import com.xinghanlaser.applet.base.auth.entity.UserDO;
import com.xinghanlaser.applet.base.auth.query.UserQuery;
import com.xinghanlaser.applet.base.auth.service.IUserService;
import com.xinghanlaser.applet.base.auth.vo.UserDataVO;
import com.xinghanlaser.applet.base.auth.vo.UserDetailVO;
import com.xinghanlaser.applet.base.auth.vo.UserPostVO;
import com.xinghanlaser.applet.base.auth.vo.UserPutVO;
import com.xinghanlaser.applet.base.common.entity.ResponseEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * <p>
 *
 * <p>
 *
 * @author: mftuo 陀铭峰
 * @since: 2024/4/29 19:10
 */
@RestController
@RequestMapping("/admin/user")
@Api(value = "用户管理", tags = "用户管理")
public class UserController  {
    @Resource
    private IUserService service;

    // 在Authorization请求头中传入 'Bearer token'
    @GetMapping
    @ApiOperation("查询用户详情")
    public ResponseEntity<UserDetailVO> userDetail(Authentication authentication) {
        return ResponseEntity.ok(service.queryUserDetail(new UserDO().setUserName((String) authentication.getPrincipal())));
    }

    @GetMapping("/page")
    @ApiOperation("根据条件分页查询用户")
    public ResponseEntity<ResponseEntity.PageResult<UserDataVO>> pageByCondition(UserQuery query){
        return service.pageByCondition(query);
    }

    @PostMapping
    @ApiOperation("新增用户")
    public ResponseEntity<Void> addUser(@RequestBody @Valid UserPostVO postVO){
        return service.addUser(postVO);
    }

    @PutMapping
    @ApiOperation("修改用户信息")
    public ResponseEntity<Void> modUser(@RequestBody @Valid UserPutVO putVO){
        return service.modUser(putVO);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除用户")
    public ResponseEntity<Void> delUser(@PathVariable("id")Long id){
        return service.delUser(id);
    }
}
